﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.LopTinChiService;
namespace eTMS.Controllers{
public class LopTinChiControllerBase : CommonController{
	public LopTinChiControllerBase()
		: base()
	{	
		this.dao = new LopTinChiServiceClient();
	}
	~LopTinChiControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private LopTinChiEntity oLopTinChi;	
	public LopTinChiEntity LopTinChiObject{
		get{
			return oLopTinChi;
		}
		set{
			oLopTinChi=value;
		}
	}
	protected LopTinChiServiceClient dao;
	public DataTable Table{
		get{
			return _Table;
		}
        set
        {
            _Table = value;
        }
	}
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oLopTinChi==null)return false;
		bool result=true;
		try{
			oLopTinChi = dao.SaveRet(oLopTinChi);
		}
		catch{
			result=false;
		}
		if(result){
			_ListLopTinChi.Add(oLopTinChi);
		}
		return result;
	}
	public bool Update(){
		if(oLopTinChi==null)return false;
		return dao.Update(oLopTinChi);
	}
	public bool Delete(){
		if(oLopTinChi==null)return false;
		bool retvalue = dao.Delete(oLopTinChi);
		if(retvalue){			
			_ListLopTinChi.Remove(oLopTinChi);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 LopTinChi_ID){
		bool retvalue = dao.DeleteByID(LopTinChi_ID);		
		if(retvalue){
			LopTinChiEntity oDeleted=FindLopTinChi(LopTinChi_ID);
			if(oDeleted!=null){
				_ListLopTinChi.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oLopTinChi = new LopTinChiEntity();
	}
    //Copy data from data row in grid to object
    public LopTinChiEntity MapDataRowToObject(DataRow row){
		LopTinChiEntity o = new LopTinChiEntity();
		 o.MaLop=Convert.ToString(row["MaLop"]);
		 if(!row.IsNull("SoSVToiDa"))
			o.SoSVToiDa=ConvertInt(row["SoSVToiDa"]);
		 if(!row.IsNull("TrangThai"))
			o.TrangThai=ConvertByte(row["TrangThai"]);
		 if(!row.IsNull("HocKy_ID"))
			o.HocKy_ID=(Int32)(row["HocKy_ID"]);
		 if(!row.IsNull("MonHoc_ID"))
			o.MonHoc_ID=(Int32)(row["MonHoc_ID"]);
		 if(!row.IsNull("NamHoc_ID"))
			o.NamHoc_ID=(Int32)(row["NamHoc_ID"]);
		 if(!row.IsNull("LopTinChi_ID"))
		    o.LopTinChi_ID=(Int32)(row["LopTinChi_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(LopTinChiEntity o,DataRow row){
		row["MaLop"]=o.MaLop;
		row["SoSVToiDa"]=o.SoSVToiDa;
		row["TrangThai"]=o.TrangThai;
		row["HocKy_ID"]=o.HocKy_ID;
		row["MonHoc_ID"]=o.MonHoc_ID;
		row["NamHoc_ID"]=o.NamHoc_ID;
		row["LopTinChi_ID"]=o.LopTinChi_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.LopTinChiObject,row);
    }
	public void LoadObject(Int32 LopTinChi_ID){
		LopTinChiEntity oLoad=FindLopTinChi(LopTinChi_ID);
		if(oLoad==null){
			oLopTinChi=this.LoadLopTinChi(LopTinChi_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oLopTinChi.IsNullLopTinChi_ID;
			if(!bNotIsNull){
				this.ListLopTinChi.Add(oLopTinChi);
			}
		}
		else{
			oLopTinChi=oLoad;
		}
	}
	#region CachedList LopTinChi
	private static List<LopTinChiEntity> _ListLopTinChi=new List<LopTinChiEntity>();
    public List<LopTinChiEntity>ListLopTinChi{
		get{
			return _ListLopTinChi;
		}
	}
	public static List<LopTinChiEntity>ListLopTinChiStatic{
		get{
			return _ListLopTinChi;
		}
	}
	public LopTinChiEntity FindLopTinChi(Int32 LopTinChi_ID){
		LopTinChiEntity oResult=this.ListLopTinChi.Find(delegate(LopTinChiEntity oFind){
			bool result=true; 
			result&=oFind.LopTinChi_ID.Equals(LopTinChi_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllLopTinChi(bool clearCache){
		if(_ListLopTinChi.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		LopTinChiServiceClient dao = new LopTinChiServiceClient();
		_ListLopTinChi=new List<LopTinChiEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public LopTinChiEntity LoadLopTinChi(Int32 LopTinChi_ID){			
		LopTinChiEntity o=dao.SelectOne(LopTinChi_ID);		
		return o;
	}
	#endregion
}
}
